

#include "battlescene.h"
#include "armiesscene.h"
#include "resourcemanager.h"
#include "log.h"
#include "messagequeue.h"
#include "gamemanager.h"
#include "gamedatamanager.h"
#include "cocos-ext.h"
#include "goldcoinscene.h"
#include "gamescene.h"

static const int CHAPTER_WIDTH = 300;
static const int CHAPTER_HEIGHT = 60;
static const int CHAPTER_SPACE = 5;

static const float SCROLL_THRESHOLD = 5.0f;

BattleScene::BattleScene():
_layer(nullptr)
{

}

BattleScene::~BattleScene() {
}

bool BattleScene::init() {
	if (!CCScene::init()) {
		return false;
	}

	_layer = CCLayer::create();
	addChild(_layer);

	CCSprite *spr = CCSprite::create("textures/ui_bg.png");
	spr->setAnchorPoint(CCPointZero);

	CCSize size = spr->getContentSize();
	spr->setScaleX(GM->screenSize.width / size.width);
	spr->setScaleY(GM->screenSize.height / size.height);
	_layer->addChild(spr);

	CCSprite* topBar = MAKE_SPRITE("top_bar.png");
	topBar->setPosition(XY_SCALE(0.0f, 536));
	topBar->setScaleX(GM->xscale);
	topBar->setAnchorPoint(ccp(0.0f, 0.5f));
	_layer->addChild(topBar);

	CCSprite* sceneTitle = MAKE_SPRITE("text_multiplayer.png");
	sceneTitle->setPosition(ccp(472, 48));
	sceneTitle->setScale(1.0f);
	topBar->addChild(sceneTitle);

	auto btnExit = RM->makeBackButton(this, menu_selector(BattleScene::onClose), topBar, true);
	btnExit->setScale(1.0f);
	btnExit->setPosition(XY_SCALE(85, 42));

	auto btnMoney = RM->makeButton("money_pad.png", nullptr, nullptr, this, menu_selector(BattleScene::onRecharge), topBar);
	btnMoney->setScale(1.0f);
	btnMoney->setPosition(ccp(900, 42));
	btnMoney->setAnchorPoint(ccp(1.0f, 0.5f));

	sprintf(buf, "%d", GDM->money);
	_labelMoney = CCLabelTTF::create(buf, "", 24);
	_labelMoney->setPosition(ccp(76, 30));
	_labelMoney->setColor(ccc3(237, 199, 143));
	_labelMoney->setAnchorPoint(ccp(0.5f, 0.5f));
	btnMoney->addChild(_labelMoney);

	cocos2d::extension::CCScale9Sprite *mainFrame = cocos2d::extension::CCScale9Sprite::create("textures/frame.png", CCRectMake(0, 0, 96, 96), CCRectMake(32, 32, 32, 32));
	mainFrame->setContentSize(CCSizeMake(865, 490));
	mainFrame->setPosition(XY_SCALE(480, 245));
	mainFrame->setScale(GM->minscale);
	_layer->addChild(mainFrame);

	CCSize pageSize = CCSizeMake(422, 460);
	cocos2d::extension::CCScale9Sprite* leftPageBg = cocos2d::extension::CCScale9Sprite::create("textures/pad_bg.png", CCRectMake(0, 0, 225, 225), CCRectMake(100, 100, 25, 25));
	leftPageBg->setContentSize(pageSize);
	leftPageBg->setPosition(ccp(222, 245));
	leftPageBg->setScale(1.0f);
	mainFrame->addChild(leftPageBg);

	cocos2d::extension::CCScale9Sprite* rightPageBg = cocos2d::extension::CCScale9Sprite::create("textures/pad_bg.png", CCRectMake(0, 0, 225, 225), CCRectMake(100, 100, 25, 25));
	rightPageBg->setContentSize(pageSize);
	rightPageBg->setPosition(ccp(423 + 222, 245));
	rightPageBg->setScale(1.0f);
	mainFrame->addChild(rightPageBg);

	CCSprite* leftPageTitleBg = MAKE_SPRITE("battle_title_frame.png");
	leftPageTitleBg->setPosition(ccp(222, 485));
	leftPageTitleBg->setScale(1.0f);
	leftPageTitleBg->setAnchorPoint(ccp(0.5f, 1.0f));
	mainFrame->addChild(leftPageTitleBg);

	CCSprite* rightPageTitleBg = MAKE_SPRITE("battle_title_frame.png");
	rightPageTitleBg->setPosition(ccp(423 + 222, 485));
	rightPageTitleBg->setScale(1.0f);
	rightPageTitleBg->setAnchorPoint(ccp(0.5f, 1.0f));
	mainFrame->addChild(rightPageTitleBg);

	CCSize pageTitleSize = leftPageTitleBg->getContentSize();

	CCSprite* leftPageTitle = MAKE_SPRITE("text_battle.png");
	leftPageTitle->setPosition(ccp(pageTitleSize.width / 2.0f, pageTitleSize.height / 2.0f));
	leftPageTitle->setScale(1.0f);
	leftPageTitleBg->addChild(leftPageTitle);

	CCSprite* rightPageTitle = MAKE_SPRITE("text_friends.png");
	rightPageTitle->setPosition(ccp(pageTitleSize.width / 2.0f, pageTitleSize.height / 2.0f));
	rightPageTitle->setScale(1.0f);
	rightPageTitleBg->addChild(rightPageTitle);

	CCMenuItemSprite* btnAutoMatch = RM->makeButton("button/match.png", nullptr, nullptr, this, menu_selector(BattleScene::onAutoMatch), leftPageBg);
	btnAutoMatch->setScale(1.0f);
	btnAutoMatch->setPosition(ccp(211, 340));

	CCSprite* matchScoreBg = MAKE_SPRITE("battle_flag_bg.png");
	matchScoreBg->setPosition(ccp(211, 240));
	matchScoreBg->setScale(1.0f);
	leftPageBg->addChild(matchScoreBg);

	CCMenuItemSprite* btnLadder = RM->makeButton("button/ladder.png", nullptr, nullptr, this, menu_selector(BattleScene::onLadder), leftPageBg);
	btnLadder->setScale(1.0f);
	btnLadder->setPosition(ccp(211, 145));

	CCSprite* ladderScoreBg = MAKE_SPRITE("battle_flag_bg.png");
	ladderScoreBg->setPosition(ccp(211, 55));
	ladderScoreBg->setScale(1.0f);
	leftPageBg->addChild(ladderScoreBg);

	// friend list
	getFriends();

	CCSize friendListSize(X_SCALE(CHAPTER_WIDTH), Y_SCALE((CHAPTER_HEIGHT + CHAPTER_SPACE) * _friends.size() - CHAPTER_SPACE));
	CCSize clippingSize = CCSizeMake(friendListSize.width, Y_SCALE(270));
	CCSize rightPageSize = rightPageBg->getContentSize();

	CCClippingNode *clippingNode = CCClippingNode::create();
	clippingNode->setAnchorPoint(ccp(0.5f, 0.5f));
	clippingNode->setPosition(ccp(rightPageSize.width / 2, rightPageSize.height / 2));
	clippingNode->setContentSize(clippingSize);

	CCPoint vertices[] = {
			ccp(0, 0),
			ccp(clippingSize.width, 0),
			ccp(clippingSize.width, clippingSize.height),
			ccp(0, clippingSize.height)
	};

	ccColor4F black = {1, 1, 1, 1};
	CCDrawNode *stencil = CCDrawNode::create();
	stencil->drawPolygon(vertices, 4, black, 1, black);
	clippingNode->setStencil(stencil);

	rightPageBg->addChild(clippingNode);

	CCLayer *layer = CCLayer::create();
	layer->setAnchorPoint(CCPointZero);
	layer->setContentSize(friendListSize);
	layer->setPosition(ccp(0, clippingSize.height - friendListSize.height));

	clippingNode->addChild(layer);
	_layerFriendList = layer;

	createFriendsList();

	return true;
}

void BattleScene::getFriends() {
	_friends.clear();

	_friends.push_back("Caesar");
	_friends.push_back("Scipio");
	_friends.push_back("Hannibal");
	_friends.push_back("Crassus");
	_friends.push_back("Sylla");
	_friends.push_back("Pompey");
	_friends.push_back("Alexander");
	_friends.push_back("Octavianus");
}

bool BattleScene::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) {
	auto pt = pTouch->getLocation();


	return true;
}

void BattleScene::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) {
	auto pt = pTouch->getLocation();
}

void BattleScene::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) {
	auto pt = pTouch->getLocation();
}

void BattleScene::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent) {
}

void BattleScene::onClose(CCObject *sender) {
	CCDirector::sharedDirector()->replaceScene(ArmiesScene::create());
}

void BattleScene::keyBackClicked() {
	onClose(nullptr);
}

void BattleScene::keyMenuClicked() {

}

void BattleScene::onLadder(CCObject *sender) {

}

void BattleScene::onAutoMatch(CCObject *sender) {
}

void BattleScene::onAutoMatchResponse(unsigned char *data, unsigned int size) {
	FormationInfo opponentFormation = FormationInfo::fromBytes((char*)data);

	GameScene* scene = GameScene::create();
	scene->setFormations(GDM->formations[0], &opponentFormation);
	scene->previousScene = MessageTarget::online_battle_scene;
	CCDirector::sharedDirector()->pushScene(scene);
}

void BattleScene::onRecharge(CCObject *sender) {
	GoldCoinScene* scene = GoldCoinScene::create();
	CCDirector::sharedDirector()->pushScene(scene);
}

void BattleScene::onEnter() {
	CCScene::onEnter();

	sprintf(buf, "%d", GDM->money);
	_labelMoney->setString(buf);

	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -1, true);
	CCDirector::sharedDirector()->getKeypadDispatcher()->addDelegate(this);
}


void BattleScene::onExit() {
	CCScene::onExit();

	CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
	CCDirector::sharedDirector()->getKeypadDispatcher()->removeDelegate(this);
}

void BattleScene::onViewFriendInfo(CCObject *sender) {

}

void BattleScene::createFriendsList() {
	_layerFriendList->removeAllChildren();

	CCSize size = _layerFriendList->getContentSize();

	float x = CHAPTER_WIDTH / 2.0f;
	float y = CHAPTER_HEIGHT / 2.0f;

	for (size_t i = 0; i < _friends.size(); i++) {
		auto pt = ccp(x, size.height - y);

		cocos2d::extension::CCScale9Sprite *itemBgNormal = cocos2d::extension::CCScale9Sprite::create("textures/item_bg.png", CCRectMake(0, 0, 80, 80), CCRectMake(20, 20, 40, 40));
		itemBgNormal->setContentSize(CCSizeMake(CHAPTER_WIDTH, CHAPTER_HEIGHT));

		cocos2d::extension::CCScale9Sprite *itemBgHighlight = cocos2d::extension::CCScale9Sprite::create("textures/highlight_item_bg.png", CCRectMake(0, 0, 80, 80), CCRectMake(20, 20, 40, 40));
		itemBgHighlight->setContentSize(CCSizeMake(CHAPTER_WIDTH, CHAPTER_HEIGHT));

		CCMenuItemSprite *button = CCMenuItemSprite::create(itemBgNormal, itemBgHighlight, nullptr, this, menu_selector(BattleScene::onViewFriendInfo));
		button->setScale(GM->minscale);
		button->setPosition(pt);

		CCMenu *menu = CCMenu::create(button, nullptr);
		menu->setPosition(CCPointZero);
		_layerFriendList->addChild(menu);

		CCLabelTTF *labelTitle = CCLabelTTF::create(_friends[i].c_str(), "", 32);
		labelTitle->setPosition(ccp(100, 36));
		labelTitle->setColor(ccc3(24, 6, 2));
		labelTitle->setAnchorPoint(ccp(0.0f, 0.5f));
		button->addChild(labelTitle);

		y += CHAPTER_HEIGHT + CHAPTER_SPACE;
	}
}